package xx.pro.shiro.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import xx.pro.shiro.entity.SysPermissions;
import xx.pro.shiro.entity.SysRole;
import xx.pro.shiro.entity.SysUser;
import xx.pro.shiro.mapper.SysPermissionsMapper;
import xx.pro.shiro.mapper.SysRoleMapper;
import xx.pro.shiro.service.SysUserService;
import xx.pro.shiro.mapper.SysUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户表(SysUser)表服务实现类
 *
 * @author halo-king
 * @since 2023-09-27 12:42:18
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    SysRoleMapper sysRoleMapper;

    @Resource
    SysPermissionsMapper sysPermissionsMapper;

    @Override
    public SysUser queryUserInfoByUserName(String userName) {
        return this.baseMapper.selectOne(new QueryWrapper<SysUser>().eq("username",userName));
    }

    @Override
    public SysUser queryUserInfoByUserInfo(String userName) {
        SysUser sysUser = this.baseMapper.selectOne(new QueryWrapper<SysUser>().eq("username",userName));
        List<SysRole> roleList = sysRoleMapper.querySysRoleByUserId(sysUser.getId());
        Set<SysPermissions> sysPermissionsSet = new HashSet<>();
        roleList.forEach(role->{
            sysPermissionsSet.addAll(this.sysPermissionsMapper.queryPermissionByRoleId(role.getId()));
        });
        sysUser.setSysRoleList(roleList);
        sysUser.setPermissionsSet(sysPermissionsSet);
        return sysUser;
    }
}

